package com.team.teamproject.ui.hot;

import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.GridView;
import android.widget.Spinner;

import com.handmark.pulltorefresh.library.PullToRefreshBase;
import com.handmark.pulltorefresh.library.PullToRefreshGridView;
import com.team.teamproject.R;
import com.team.teamproject.adapter.firstHotAdapter;
import com.team.teamproject.bean.Filters;
import com.team.teamproject.bean.hotitems;
import com.team.teamproject.utils.HttpUtils;

import java.util.List;

/**
 * Created by Administrator on 2016/11/5.
 */

public class FragmentReMen extends Fragment {

    private Spinner spinner_duixiang,spinner_changhe,spinner_gexing,spinner_jiage;
    private String taburl="http://api.liwushuo.com/v2/search/item_filter";
    private String Hid="1";
    private String  offset="0";
    private String duixiangurl="http://api.liwushuo.com/v2/item_subcategories/";
         private String  duixiangurl2=  "/items?limit=20&offset=";
      private List<Filters> lists;
    private List<hotitems> listsitems;
     private firstHotAdapter adapter;
    private Handler handler=new Handler(){
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            switch (msg.what)
            {
                case 1:
                    listsitems = (List<hotitems>) msg.obj;
                    adapter=new firstHotAdapter(getActivity(),listsitems);
                    myGridView.setAdapter(adapter);
                    adapter.notifyDataSetChanged();
                    break;
                case 2:
                    lists= (List<Filters>) msg.obj;
                    break;
            }
        }
    };
    private PullToRefreshGridView myPullToRefreshGridView;
    private  GridView myGridView;
    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_remen, null);
        spinner_duixiang= (Spinner) view.findViewById(R.id.spinner_duixiang);
        spinner_changhe= (Spinner) view.findViewById(R.id.spinner_changhe);
        spinner_gexing= (Spinner) view.findViewById(R.id.spinner_gexing);
        spinner_jiage= (Spinner) view.findViewById(R.id.spinner_jiage);

        myPullToRefreshGridView= (PullToRefreshGridView) view.findViewById(R.id.pull_refresh_grid);
        myGridView=myPullToRefreshGridView.getRefreshableView();
        getdata();
         getHotData();
        //刷新
        myPullToRefreshGridView.setOnRefreshListener(new PullToRefreshBase.OnRefreshListener2<GridView>() {
            @Override
            public void onPullDownToRefresh(PullToRefreshBase<GridView> refreshView) {

                myPullToRefreshGridView.onRefreshComplete();
            }

            @Override
            public void onPullUpToRefresh(PullToRefreshBase<GridView> refreshView) {
                   getHotData();
                offset+=20;
                myPullToRefreshGridView.onRefreshComplete();
            }
        });
         spinner_duixiang.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
             @Override
             public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
                 switch (position)
                 {
                     case 1:
                     case 2:
                     case 3:
                     case 4:
                     case 5:
                     case 6:
                     case 7:
                         Hid=lists.get(0).getChannels().get(position-1).getId();
                         getHotData();
                         break;
                 }

             }

             @Override
             public void onNothingSelected(AdapterView<?> parent) {

             }
         });
        spinner_changhe.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
                switch (position)
                {
                    case 0:
                        Hid=3+"";
                        getHotData();
                        break;
                    case 1:
                    case 2:
                    case 3:
                    case 4:
                    case 5:
                    case 6:
                    case 7:
                    case 8:
                    case 9:
                        Hid=lists.get(1).getChannels().get(position-1).getId();
                        Log.i("aaa","---->Hid"+Hid);
                        getHotData();
                        break;
                }

            }

            @Override
            public void onNothingSelected(AdapterView<?> parent) {

            }
        });
        spinner_gexing.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
                switch (position)
                {
                    case 0:
                        Hid=2+"";
                        getHotData();
                        break;
                    case 1:
                    case 2:
                    case 3:
                    case 4:
                    case 5:
                    case 6:
                    case 7:
                        Hid=lists.get(2).getChannels().get(position-1).getId();
                        Log.i("aaa","---->Hid"+Hid);
                        getHotData();
                        break;
                }

            }

            @Override
            public void onNothingSelected(AdapterView<?> parent) {

            }
        });
        spinner_jiage.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
                switch (position)
                {
                    case 0:
                        Hid=4+"";
                        getHotData();
                        break;
                    case 1:
                    case 2:
                    case 3:
                    case 4:
                    case 5:

                        Hid=lists.get(3).getChannels().get(position-1).getId();
                        Log.i("aaa","---->Hid"+Hid);
                        getHotData();
                        break;
                }

            }

            @Override
            public void onNothingSelected(AdapterView<?> parent) {

            }
        });

      myGridView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
          @Override
          public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
              Intent intent=new Intent(getActivity(),HotDetailesActivity.class);
              intent.putExtra("id",listsitems.get(position).getId());
              startActivity(intent);
          }
      });
        return view;


    }

    private void getHotData() {
        new Thread(new Runnable() {
            @Override
            public void run() {
                String jsonduixian=HttpUtils.getHttpUtils().getJson(duixiangurl+Hid+duixiangurl2+offset);

                if (jsonduixian!=null&&jsonduixian.length()>0)
                {
                  //  listsitems=ParseJson.fromhotitems(jsonduixian);
                    Message msg=Message.obtain();
                    msg.what=1;
                    msg.obj=listsitems;
                    handler.sendMessage(msg);

                }
            }
        }).start();
    }

    private void getdata() {
        new Thread(new Runnable() {
            @Override
            public void run() {
                String json = HttpUtils.getHttpUtils().getJson(taburl);
                // Log.i("abc","----->"+json.toString());
                if (json != null && json.length() > 0) {
                    //lists = ParseJson.fromhot(json);
                    Message msg=Message.obtain();
                    msg.what=2;
                    msg.obj=lists;
                    handler.sendMessage(msg);
                }

            }
        }).start();
    }

}
